﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Data

Public Class frmVichama

    Private strConexion As String = ""
    Private sLenguaje As String = ""
    Private dtTablas As DataTable = Nothing


    Private Sub btnDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDB.Click
        CargarBD()
    End Sub

    Private Sub cboBaseDatos_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBaseDatos.SelectionChangeCommitted
        Try

            Me.dtTablas.Rows.Clear()

            If Me.cboBaseDatos.SelectedIndex = 0 Then
                For i As Integer = 0 To 5
                    Dim drTabla As DataRow = Me.dtTablas.NewRow
                    drTabla.Item("Id") = 0
                    drTabla.Item("Nombre") = ""
                    Me.dtTablas.Rows.Add(drTabla)
                Next
                Me.dtTablas.AcceptChanges()
                Exit Sub
            End If

            Me.strConexion = "Data Source=" & Me.txtServidor.Text & ";Initial Catalog=" & CType(Me.cboBaseDatos.SelectedItem, DataRowView).Item(1).ToString & ";User Id=" & Me.txtUsuario.Text & ";Password=" & Me.txtClave.Text & ";"
            Me.Cursor = Cursors.WaitCursor

            Dim dt As DataTable = GetTables()
            If dt.Rows.Count = 0 Then
                For i As Integer = 0 To 5
                    Dim drTabla As DataRow = Me.dtTablas.NewRow
                    drTabla.Item("Id") = 0
                    drTabla.Item("Nombre") = ""
                    Me.dtTablas.Rows.Add(drTabla)
                Next
            Else
                For Each dr As DataRow In dt.Rows
                    Dim drTablas As DataRow = Me.dtTablas.NewRow
                    drTablas.Item("Id") = dr.Item("object_id")
                    drTablas.Item("Nombre") = dr.Item("name")
                    Me.dtTablas.Rows.Add(drTablas)
                Next
            End If
            Me.dtTablas.AcceptChanges()

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Arrow
        End Try
    End Sub




    Public Sub CargarBD()
        Try

            If Me.txtServidor.TextLength = 0 Then Throw New Exception("No ingreso el Servidor de Base de Datos, porfavor verifique.")
            If Me.txtUsuario.TextLength = 0 Then Throw New Exception("No ingreso el Usuario de Base de Datos, porfavor verifique.")
            If Me.txtClave.TextLength = 0 Then Throw New Exception("No ingreso la Clave de Base de Datos, porfavor verifique.")

            Me.strConexion = "Data Source=" & Me.txtServidor.Text & ";Initial Catalog=master;User Id=" & Me.txtUsuario.Text & ";Password=" & Me.txtClave.Text & ";"

            Me.Cursor = Cursors.WaitCursor

            Dim dtDbs As DataTable = GetDataBases()

            Dim dr As DataRow
            dr = dtDbs.NewRow()
            dr(0) = 0
            dr(1) = "Seleccione una Base de Datos"
            dtDbs.Rows.InsertAt(dr, 0)
            dtDbs.AcceptChanges()

            Me.cboBaseDatos.DataSource = dtDbs
            Me.cboBaseDatos.DisplayMember = dtDbs.Columns(1).Caption
            Me.cboBaseDatos.ValueMember = dtDbs.Columns(0).Caption

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Arrow
        End Try
    End Sub




#Region "Propiedades BD"
    Private Function GetDataBases() As DataTable
        Try
            Dim sql As String = "SELECT database_id, name FROM sys.databases"
            Dim dt As DataTable = ListSql(sql)

            Return dt
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetTables() As DataTable
        Try
            Dim sql As String = "SELECT object_id, name FROM sys.Tables ORDER BY name"
            Dim dt As DataTable = ListSql(sql)

            Return dt
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetColumns(ByVal object_id As Integer) As DataTable
        Try
            Dim sql As String = "SELECT * FROM sys.columns where object_id = " & object_id
            Dim dt As DataTable = ListSql(sql)

            Return dt
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetColumnsSinAuditoria(ByVal object_id As Integer) As DataTable
        Try
            Dim sql As String = "SELECT * FROM sys.columns where [name] not like '[_]%' and object_id = " & object_id
            Dim dt As DataTable = ListSql(sql)

            Return dt
        Catch ex As Exception
            Throw ex
        End Try
    End Function


    Private Function GetPrimaryKey(ByVal object_id As Integer) As DataRow
        Try
            Dim sql As String = "SELECT * FROM sys.columns where name in (SELECT TOP 1 COL_NAME(t0.OBJECT_ID,t0.column_id) FROM sys.index_columns t0 where object_id = " & object_id & ") and object_id = " & object_id
            Dim dt As DataTable = ListSql(sql)

            If dt.Rows.Count = 1 Then
                Return dt.Rows(0)
            Else
                Return Nothing
                'Throw New Exception("GetPrimaryKey")
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetFormatColumnSQl(ByRef drColumns As DataRow) As String
        Try

            Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

            Dim array1() As String = New String() {"varchar", "char", "nvarchar", "nchar", "float"}
            Dim array2() As String = New String() {"decimal", "numeric"}

            For Each str As String In array1
                If drType.Item("name").ToString = str Then
                    Return drType.Item("name").ToString.Trim & "(" & drColumns.Item("max_length").ToString.Trim & ")"
                End If
            Next

            For Each str As String In array2
                If drType.Item("name").ToString = str Then
                    Return drType.Item("name").ToString.Trim & "(" & drColumns.Item("precision").ToString.Trim & "," & drColumns.Item("scale").ToString.Trim & ")"
                End If
            Next

            Return drType.Item("name").ToString.Trim

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetFormatColumnData(ByRef drColumns As DataRow, Optional ByVal listar As Boolean = False) As String
        Try

            Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

            Dim array1() As String = New String() {"varchar", "char", "nvarchar", "nchar", "float"}
            Dim array2() As String = New String() {"decimal", "numeric"}

            For Each str As String In array1
                If drType.Item("name").ToString = str Then

                    Return drType.Item("name").ToString.Trim & "," & drColumns.Item("max_length").ToString.Trim

                End If

            Next

            For Each str As String In array2
                If drType.Item("name").ToString = str Then

                    '  Return drType.Item("name").ToString.Trim & "(" & drColumns.Item("precision").ToString.Trim & "," & drColumns.Item("scale").ToString.Trim & ")"

                    Return drType.Item("name").ToString.Trim & "," & drColumns.Item("precision").ToString.Trim



                End If
            Next

            If listar = True Then
                If drType.Item("name").ToString.ToUpper = "BIT" Then
                    Return "Integer"
                End If
            End If


            Return drType.Item("name").ToString.Trim

        Catch ex As Exception
            Throw ex
        End Try
    End Function



    Private Function GetTypeColumn(ByVal system_type_id As Integer) As DataRow
        Try
            Dim sql As String = "SELECT * FROM sys.types where system_type_id = " & system_type_id
            Dim dt As DataTable = ListSql(sql)

            If dt.Rows.Count > 0 Then
                Return dt.Rows(0)
            Else
                Throw New Exception("GetTypeColumn")
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetTranslateType(ByVal type As String, Optional ByVal language As String = "VBNET", Optional ByVal Listar As Boolean = False) As String
        Try
            Dim translate As String = ""

            If language = "VBNET" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        translate = "String"
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        translate = "String"
                    Case "tinyint", "smallint", "int"
                        translate = "Integer"
                    Case "bigint"
                        translate = "Long"
                    Case "bit"
                        translate = "Boolean"
                        If Listar = True Then
                            translate = "Integer"
                        End If

                    Case "decimal", "numeric", "money", "smallmoney"
                        translate = "Double"
                    Case "float", "real" 'Approximate Numerics
                        translate = "Double"
                    Case "smalldatetime", "datetime" 'Date and Time
                        translate = "Date"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        translate = "String"
                    Case Else 'Other Data Types
                        translate = "String"
                End Select

            ElseIf language = "CSHARP" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        translate = "string"
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        translate = "string"
                    Case "tinyint", "smallint", "int", "bigint"
                        translate = "int"
                    Case "bit"
                        translate = "bool"
                    Case "decimal", "numeric", "money", "smallmoney"
                        translate = "double"
                    Case "float", "real" 'Approximate Numerics
                        translate = "double"
                    Case "smalldatetime", "datetime" 'Date and Time
                        translate = "Date"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        translate = "String"
                    Case Else 'Other Data Types
                        translate = "String"
                End Select

            End If
            Return translate
        Catch ex As Exception
            Throw ex
        End Try
    End Function
   
    Private Function GetDefaultValue(ByVal type As String, Optional ByVal language As String = "VBNET") As String
        Try
            Dim translate As String = ""

            If language = "VBNET" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        translate = """"""
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        translate = """"""
                    Case "tinyint", "smallint", "int"
                        translate = "-1"
                    Case "bigint"
                        translate = "0"
                    Case "bit"
                        translate = "False"
                    Case "decimal", "numeric", "money", "smallmoney"
                        translate = "0.0"
                    Case "float", "real" 'Approximate Numerics
                        translate = "0.0"
                    Case "smalldatetime", "datetime" 'Date and Time
                        translate = "Date.Now"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        translate = """"""
                    Case Else 'Other Data Types
                        translate = ""
                End Select

            ElseIf language = "CSHARP" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        translate = "''"
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        translate = "''"
                    Case "tinyint", "smallint", "int", "bigint"
                        translate = "0"
                    Case "bit"
                        translate = "False"
                    Case "decimal", "numeric", "money", "smallmoney"
                        translate = "0.0"
                    Case "float", "real" 'Approximate Numerics
                        translate = "0.0"
                    Case "smalldatetime", "datetime" 'Date and Time
                        translate = "Date.Now()"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        translate = "''"
                    Case Else 'Other Data Types
                        translate = ""
                End Select
            ElseIf language = "MSSQL" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        translate = "''"
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        translate = "''"
                    Case "tinyint", "smallint", "int"
                        translate = "-1"
                    Case "bigint"
                        translate = "0"
                    Case "bit"
                        translate = "False"
                    Case "decimal", "numeric", "money", "smallmoney"
                        translate = "0.0"
                    Case "float", "real" 'Approximate Numerics
                        translate = "0.0"
                    Case "smalldatetime", "datetime" 'Date and Time
                        translate = "Date.Now()"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        translate = "''"
                    Case Else 'Other Data Types
                        translate = ""
                End Select
            End If

            Return translate
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetConvertType(ByVal type As String, Optional ByVal language As String = "VBNET") As String
        Try
            Dim convert As String = ""

            If language = "VBNET" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        convert = "CStr"
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        convert = "CStr"
                    Case "tinyint", "smallint", "int"
                        convert = "CInt"
                    Case "bigint"
                        convert = "CLong"
                    Case "bit"
                        convert = "CBool"
                    Case "decimal", "numeric", "money", "smallmoney"
                        convert = "CDbl"
                    Case "float", "real" 'Approximate Numerics
                        convert = "CDbl"
                    Case "smalldatetime", "datetime" 'Date and Time
                        convert = "CDate"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        convert = "CStr"
                    Case Else 'Other Data Types
                        convert = ""
                End Select

            ElseIf language = "CSHARP" Then
                Select Case type
                    Case "text", "varchar", "char" 'Character Strings
                        convert = "Convert.ToString"
                    Case "ntext", "nvarchar", "nchar" 'Unicode Character Strings
                        convert = "Convert.ToString"
                    Case "tinyint", "smallint", "int", "bigint"
                        convert = "int.Parse"
                    Case "bit"
                        convert = "bool.Parse"
                    Case "decimal", "numeric", "money", "smallmoney"
                        convert = "double.Parse"
                    Case "float", "real" 'Approximate Numerics
                        convert = "double.Parse"
                    Case "smalldatetime", "datetime" 'Date and Time
                        convert = "DateTime.Parse"
                    Case "binary", "varbinary", "image" 'Binary Strings
                        convert = "Convert.ToString"
                    Case Else 'Other Data Types
                        convert = ""
                End Select

            End If
            Return convert
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function ListSql(ByVal sql As String, Optional ByVal params() As SqlParameter = Nothing) As DataTable
        Try
            Dim cnx As New SqlConnection(Me.strConexion)

            Dim cmd As New SqlCommand(sql, cnx)
            cmd.CommandType = CommandType.Text

            Dim dad As New SqlDataAdapter(cmd)

            If Not params Is Nothing Then
                For i As Integer = 0 To params.Length - 1
                    dad.SelectCommand.Parameters.Add(params(i))
                Next
            End If

            Dim dt As New DataTable
            dad.Fill(dt)

            Return dt

        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try

    End Function


#End Region

    Private Sub frmVichama_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            dtTablas = New DataTable
            dtTablas.Columns.Add("Id", System.Type.GetType("System.Int32"))
            dtTablas.Columns.Add("Nombre", System.Type.GetType("System.String"))

            For i As Integer = 0 To 5
                Dim drTabla As DataRow = Me.dtTablas.NewRow
                drTabla.Item("Id") = 0
                drTabla.Item("Nombre") = ""
                Me.dtTablas.Rows.Add(drTabla)
            Next

            Dim chkEdit As New DataGridViewCheckBoxColumn
            With chkEdit
                .TrueValue = 1
                .FalseValue = 0
                .Name = ""
                .ReadOnly = False
                .Width = 20
            End With
            Me.dgvTablas.Columns.Insert(0, chkEdit)

            For Each col As DataGridViewColumn In Me.dgvTablas.Columns
                col.SortMode = DataGridViewColumnSortMode.NotSortable
            Next

            With Me.dgvTablas

                .RowTemplate.Resizable = DataGridViewTriState.False

                .ColumnHeadersDefaultCellStyle.ForeColor = Color.White
                .ColumnHeadersDefaultCellStyle.BackColor = Color.Navy

                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
                .MultiSelect = False
                .AllowDrop = False
                .RowHeadersVisible = False

                .ReadOnly = False
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False

                .DataSource = Me.dtTablas

                .Columns("Id").Visible = False
                .Columns("Id").ReadOnly = True
                .Columns("Nombre").Width = .Width - 40
                .Columns("Nombre").ReadOnly = True

            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerar.Click
        Try

            'Validaciones
            '=====================================================================================================
            If Me.txtCarpetaDestino.TextLength = 0 Then Throw New Exception("No ingreso la Carpeta Destino donde se generaran los archivo, porfavor verifique.")

            If Me.txtServidor.TextLength = 0 Then Throw New Exception("No ingreso el Servidor de Base de Datos, porfavor verifique.")
            If Me.txtUsuario.TextLength = 0 Then Throw New Exception("No ingreso el Usuario de Base de Datos, porfavor verifique.")
            If Me.txtClave.TextLength = 0 Then Throw New Exception("No ingreso la Clave de Base de Datos, porfavor verifique.")

            If Me.cboBaseDatos.DataSource Is Nothing Then Throw New Exception("No se cargo la lista de Base de Datos, porfavor verifique.")
            If Me.cboBaseDatos.SelectedIndex = 0 Then Throw New Exception("No se selecciono la Base de Datos, porfavor verifique.")
            '=====================================================================================================

            Me.strConexion = "Data Source=" & Me.txtServidor.Text & ";Initial Catalog=" & CType(Me.cboBaseDatos.SelectedItem, DataRowView).Item(1).ToString & ";User Id=" & Me.txtUsuario.Text & ";Password=" & Me.txtClave.Text & ";"

            Me.Cursor = Cursors.WaitCursor

            Me.txtTexto.Clear()

            ' If Me.rdbVBNET.Checked Then
            Me.sLenguaje = "VBNET"
            '  ElseIf Me.rdbCSHARP.Checked Then
            'Me.sLenguaje = "CSHARP"
            'Else
            ' Throw New Exception("No existe el lenguaje seleccionado, porfavor verifique.")
            'End If

            Me.txttexto.Text = Me.txttexto.Text & "Inicio de la Generacion de Codigo " & vbCrLf

            'Dim dtTables As DataTable = GetTables()
            '==================================================================================
            Dim dtTables As New DataTable
            dtTables.Columns.Add("object_id", System.Type.GetType("System.Int32"))
            dtTables.Columns.Add("name", System.Type.GetType("System.String"))
            For Each drv As DataGridViewRow In Me.dgvTablas.Rows
                If CType(drv.Cells(0).Value, Boolean) = True Then 'Si esta con CHECK
                    Dim drTable As DataRow = dtTables.NewRow
                    drTable.Item("object_id") = CInt(drv.Cells("Id").Value)
                    drTable.Item("name") = drv.Cells("Nombre").Value.ToString
                    dtTables.Rows.Add(drTable)
                End If
            Next
            dtTables.AcceptChanges()
            '==================================================================================

            For Each drTable As DataRow In dtTables.Rows

                If Me.chk_sql.Checked Then GenerateScriptsSQL(drTable)

                If Me.chk_ent.Checked Then
                    If Me.sLenguaje = "VBNET" Then
                        GenerateClassBE_VBNET(drTable)
                        GenerateClassLE_VBNET(drTable)
                        GenerateClassDE_VBNET(drTable)
                    ElseIf Me.sLenguaje = "CSHARP" Then
                        'GenerateClassBE_CSHARP(drTable)
                    End If
                End If

                    '    If Me.chkGenerarLn.Checked Then
                    '        If Me.sLenguaje = "VBNET" Then
                    '            GenerateClassLN_VBNET(drTable)
                    '        ElseIf Me.sLenguaje = "CSHARP" Then
                    '            GenerateClassLN_CSHARP(drTable)
                    '        End If
                    '    End If

            Next

            If Me.chk_sql.Checked Then Me.txttexto.Text = Me.txttexto.Text & "- Se genero scripts de SQL" & vbCrLf
            'If Me.chkGenerarBe.Checked Then Me.txttexto.Text = Me.txttexto.Text & "- Se genero clases de BE" & vbCrLf
            'If Me.chkGenerarLn.Checked Then Me.txttexto.Text = Me.txttexto.Text & "- Se genero clases de LN" & vbCrLf

            'Me.txttexto.Text = Me.txttexto.Text & "Fin de la Generacion de Codigo"

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Arrow
        End Try
    End Sub

    Private Sub GenerateScriptsSQL(ByRef drTable As DataRow)
        Try

            Dim strUbicacion As String = Me.txtCarpetaDestino.Text & "\SQL"

            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If

            Dim objReader As New StreamWriter(strUbicacion & "\" & drTable.Item("name").ToString & ".sql")
            objReader.WriteLine("set ANSI_NULLS ON")
            objReader.WriteLine("set QUOTED_IDENTIFIER ON")
            objReader.WriteLine("GO")

            objReader.WriteLine("")

            'Obtenemos las columnas de la tabla
            Dim dtColumns As DataTable = GetColumns(CInt(drTable.Item("object_id")))
            Dim dtColumnsSinAuditoria As DataTable = GetColumnsSinAuditoria(CInt(drTable.Item("object_id")))


            'Obtiene el PK de la tabla, en caso de no tener PK se obtiene la primera columna
            Dim drPrimaryKey As DataRow = GetPrimaryKey(CInt(drTable.Item("object_id")))
            Dim nameColumnPK As String = ""
            If drPrimaryKey Is Nothing Then
                drPrimaryKey = dtColumns.Rows(0)
                nameColumnPK = dtColumns.Rows(0).Item("name").ToString
            Else
                nameColumnPK = drPrimaryKey.Item("name").ToString
            End If
            nameColumnPK = nameColumnPK.Substring(0, 1).ToUpper & nameColumnPK.Substring(1)

            Dim nameTable As String = drTable.Item("name").ToString
            nameTable = nameTable.Substring(0, 1).ToUpper & nameTable.Substring(1)

            Dim head As String = ""
            Dim foot As String = ""

            'Insert
            objReader.WriteLine("CREATE PROCEDURE Up_" & nameTable & "_Insertar")
            objReader.WriteLine("@opcion AS VARCHAR(50)='ins'")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1

                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(",@" & drColumns.Item("name").ToString & " AS " & GetFormatColumnSQl(drColumns).ToUpper & "=" & GetDefaultValue(drType.Item("name"), "MSSQL"))

            Next
            objReader.WriteLine(",@_usuario int=-1 ")
            objReader.WriteLine(",@_fecha  bigint=0")
            objReader.WriteLine(",@variable_out int=-1 output")
            objReader.WriteLine("AS")
            objReader.WriteLine("BEGIN")

            objReader.WriteLine("if @opcion='ins' begin ")

            head = "INSERT INTO " & nameTable & " ("
            For i As Integer = 1 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                head = head & drColumns.Item("name").ToString.Trim & ","
            Next
            head = head & " _usu_Cre,_fec_Cre,_usu_mod,_fec_mod,_flg_Eli)"
            objReader.WriteLine(head)

            foot = "VALUES ("
            For i As Integer = 1 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                foot = foot & "@" & drColumns.Item("name").ToString.Trim & ","
            Next
            foot = foot & " @_usuario,@_fecha,@_usuario,@_fecha,0)"
            objReader.WriteLine(foot)

            objReader.WriteLine("SET @" & nameColumnPK & " = @@IDENTITY")
            objReader.WriteLine(" set @variable_out=@" & nameColumnPK)
            objReader.WriteLine("end ")


            objReader.WriteLine("END")
            objReader.WriteLine("GO")

            objReader.WriteLine("")

            'Update
            objReader.WriteLine("CREATE PROCEDURE Up_" & nameTable & "_Actualizar")
            objReader.WriteLine("@opcion AS VARCHAR(50)='upd'")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(",@" & drColumns.Item("name").ToString & " AS " & GetFormatColumnSQl(drColumns).ToUpper & "=" & GetDefaultValue(drType.Item("name"), "MSSQL"))

            Next
            objReader.WriteLine(",@_usuario int=-1 ")
            objReader.WriteLine(",@_fecha  bigint=0")

            objReader.WriteLine(",@variable_out int=-1 output")
            objReader.WriteLine("AS")
            objReader.WriteLine("BEGIN")

            objReader.WriteLine("if @opcion='UPD' begin ")
            objReader.WriteLine("UPDATE " & nameTable)
            head = "SET "
            For i As Integer = 1 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                If Not (drColumns.Item("name").ToString.Substring(1, 1) = "_") Then
                    head = head & drColumns.Item("name").ToString.Trim & " = @" & drColumns.Item("name").ToString.Trim & ","
                    objReader.WriteLine(head)
                    head = ""
                End If

            Next
            objReader.WriteLine("_usu_mod=@_usuario,")
            objReader.WriteLine("_fec_mod=@_fecha")

            objReader.WriteLine("WHERE " & dtColumns.Rows(0).Item("name").ToString.Trim & " = @" & dtColumns.Rows(0).Item("name").ToString.Trim)
            objReader.WriteLine("END --end if ")

            objReader.WriteLine(" set @variable_out=@" & nameColumnPK)

            objReader.WriteLine("END")

            objReader.WriteLine("GO")

            objReader.WriteLine("")

            'Delete
            objReader.WriteLine("CREATE PROCEDURE up_" & nameTable & "_Eliminar")
            objReader.WriteLine("@opcion AS VARCHAR(50)='DEL'")
            objReader.WriteLine(",@" & nameColumnPK & " AS " & GetFormatColumnSQl(drPrimaryKey).ToUpper)
            objReader.WriteLine(",@_usuario int=-1 ")
            objReader.WriteLine(",@_fecha  bigint=0")
            objReader.WriteLine(",@variable_out int=-1 output")
            objReader.WriteLine("AS")
            objReader.WriteLine("BEGIN")
            objReader.WriteLine("if @opcion='DEL' or @opcion='' begin ")
            objReader.WriteLine("UPDATE  " & nameTable)
            objReader.WriteLine("SET _FLG_ELI=1, ")
            objReader.WriteLine("_usu_mod=@_usuario,")
            objReader.WriteLine("_fec_mod=@_fecha")
            objReader.WriteLine("WHERE " & nameColumnPK & " = @" & nameColumnPK)
            objReader.WriteLine(" set @variable_out=@" & nameColumnPK)
            objReader.WriteLine("END --end if ")
            objReader.WriteLine("")
            objReader.WriteLine("if @opcion='BORRAR' begin ")
            objReader.WriteLine("DELETE FROM " & nameTable)
            objReader.WriteLine("WHERE " & nameColumnPK & " = @" & nameColumnPK)
            objReader.WriteLine(" set @variable_out=@" & nameColumnPK)
            objReader.WriteLine("END --end if ")
            objReader.WriteLine("")
            objReader.WriteLine("END")
            objReader.WriteLine("GO")

            objReader.WriteLine("")

            'List
            objReader.WriteLine("CREATE PROCEDURE Up_" & nameTable & "_Listar")
            objReader.WriteLine("@opcion AS VARCHAR(50)=''")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                objReader.WriteLine(",@" & drColumns.Item("name").ToString & " AS " & GetFormatColumnSQl(drColumns).ToUpper & "=" & GetDefaultValue(drType.Item("name"), "MSSQL"))
            Next
            objReader.WriteLine(",@_usuario int=-1 ")
            objReader.WriteLine(",@_fecha  bigint=0")
            objReader.WriteLine("AS")
            objReader.WriteLine("BEGIN")
            objReader.WriteLine("if @opcion='' begin ")

            objReader.WriteLine("SELECT ")
            head = ""

            For i As Integer = 0 To dtColumns.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                head = "isnull(" & head & drColumns.Item("name").ToString.Trim & ", " & GetDefaultValue(drType.Item("name"), "MSSQL") & ") as " & drColumns.Item("name").ToString.Trim & IIf(i = dtColumns.Rows.Count - 1, "", ",")
                objReader.WriteLine(head)
                head = ""
            Next
            objReader.WriteLine("FROM " & nameTable)

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                Select Case drType.Item("name")
                    Case "varchar"
                        head = IIf(i = 0, "where ", "and ") & "((" & drColumns.Item("name").ToString.Trim & " like @" & drColumns.Item("name").ToString.Trim & " +'%') or ( len(@" & drColumns.Item("name").ToString.Trim & ")=0))"
                        objReader.WriteLine(head)


                    Case "tinyint", "smallint", "int", "bigint"
                        head = IIf(i = 0, "where ", "and ") & "((" & drColumns.Item("name").ToString.Trim & " = @" & drColumns.Item("name").ToString.Trim & ") or ( @" & drColumns.Item("name").ToString.Trim & "=-1))"
                        objReader.WriteLine(head)


                End Select

            Next

            objReader.WriteLine("END --end if ")
            objReader.WriteLine("")
            objReader.WriteLine("END")
            objReader.WriteLine("GO")

            objReader.WriteLine("")

            'Get
            objReader.WriteLine("CREATE PROCEDURE Up_" & nameTable & "_Obtener")
            objReader.WriteLine("@" & nameColumnPK & " AS " & GetFormatColumnSQl(drPrimaryKey).ToUpper)
            objReader.WriteLine("AS")
            objReader.WriteLine("BEGIN")
            objReader.WriteLine("SELECT TOP 1 ")
            For i As Integer = 0 To dtColumns.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                objReader.WriteLine("isnull(" & drColumns.Item("name").ToString.Trim & ", " & GetDefaultValue(drType.Item("name"), "MSSQL") & ") as " & drColumns.Item("name").ToString.Trim & IIf(i = dtColumns.Rows.Count - 1, "", ","))
                '                objReader.WriteLine(drColumns.Item("name").ToString.Trim & IIf(i = dtColumns.Rows.Count - 1, "", ","))
            Next
            objReader.WriteLine("FROM " & nameTable)
            objReader.WriteLine("WHERE " & nameColumnPK & " = @" & nameColumnPK)
            objReader.WriteLine("END")
            objReader.WriteLine("GO")

            objReader.WriteLine("")






            objReader.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub GenerateClassBE_VBNET(ByRef drTable As DataRow)
        Try

            Dim nameTable As String = drTable.Item("name").ToString
            Dim strNamespace As String = ""
            Dim strtabla As String = ""
            Try
                strNamespace = NamespaceTraduccion(nameTable.Split("_")(0))
                strtabla = nameTable.Split("_")(1)
            Catch ex As Exception
                strNamespace = "general"
                strtabla = nameTable
            End Try


            Dim strUbicacion As String = Me.txtCarpetaDestino.Text & "\BE"

            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If

            strUbicacion = strUbicacion & "\" & strNamespace
            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If



            Dim objReader As New StreamWriter(strUbicacion & "\" & strtabla & ".vb")


       


            nameTable = nameTable.Substring(0, 1).ToUpper & nameTable.Substring(1)

            Dim dtColumns As DataTable = GetColumns(CInt(drTable.Item("object_id")))
            Dim dtColumnsSinAuditoria As DataTable = GetColumnsSinAuditoria(CInt(drTable.Item("object_id")))



            objReader.WriteLine("Namespace " + strNamespace)
            objReader.WriteLine("Public Class " & strtabla)
            objReader.WriteLine(vbTab & "Inherits Auditoria.Transaccion")

            objReader.WriteLine("")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)

                Dim valueColumn As String = GetDefaultValue(drType.Item("name"))

                objReader.WriteLine(vbTab & "Private v" & nameColumn & " As " & GetTranslateType(drType.Item("name")) & " = " & valueColumn)


                If Microsoft.VisualBasic.Right(nameColumn, 2).ToUpper = "ID" And i > 0 Then
                    objReader.WriteLine(vbTab & "Private v" & nameColumn.Substring(0, nameColumn.Length - 2) & "Nombre As string =""""")
                End If

             

            Next

            objReader.WriteLine("")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)

                Dim typeColumn As String = GetTranslateType(drType.Item("name"))

                objReader.WriteLine(vbTab & "Public Property " & nameColumn & "() As " & typeColumn)
                objReader.WriteLine(vbTab & vbTab & "Get")
                objReader.WriteLine(vbTab & vbTab & vbTab & "Return v" & nameColumn)
                objReader.WriteLine(vbTab & vbTab & "End Get")
                objReader.WriteLine(vbTab & vbTab & "Set(ByVal Value As " & typeColumn & ")")
                objReader.WriteLine(vbTab & vbTab & vbTab & "v" & nameColumn & " = Value")
                objReader.WriteLine(vbTab & vbTab & "End Set")
                objReader.WriteLine(vbTab & "End Property")



                objReader.WriteLine("")

                If Microsoft.VisualBasic.Right(nameColumn, 2).ToUpper = "ID" And i > 0 Then

                    objReader.WriteLine(vbTab & "Public Property " & nameColumn.Substring(0, nameColumn.Length - 2) & "Nombre() as string")
                    objReader.WriteLine(vbTab & vbTab & "Get")
                    objReader.WriteLine(vbTab & vbTab & vbTab & "Return v" & nameColumn.Substring(0, nameColumn.Length - 2) & "Nombre")
                    objReader.WriteLine(vbTab & vbTab & "End Get")
                    objReader.WriteLine(vbTab & vbTab & "Set(ByVal Value As string)")
                    objReader.WriteLine(vbTab & vbTab & vbTab & "v" & nameColumn.Substring(0, nameColumn.Length - 2) & "Nombre" & " = Value")
                    objReader.WriteLine(vbTab & vbTab & "End Set")
                    objReader.WriteLine(vbTab & "End Property")


                    objReader.WriteLine("")

                End If

                If drType.Item("name") = "bigint" Then


                    objReader.WriteLine(vbTab & "Public readonly  Property " & nameColumn & "Str() as string")
                    objReader.WriteLine(vbTab & vbTab & "Get")
                    objReader.WriteLine(vbTab & vbTab & vbTab & "Return System.DateTime.FromFileTime(" & nameColumn & ").ToShortDateString")

                    objReader.WriteLine(vbTab & vbTab & "End Get")
                    objReader.WriteLine(vbTab & "End Property")

                    objReader.WriteLine("")

                End If





            Next

            'Definimos Metodos Constructores

            'Metodo Construsctor Limpio
            objReader.WriteLine(vbTab & "Sub New()")
            objReader.WriteLine(vbTab & "End Sub")

            objReader.WriteLine("")

            'Metodo Construsctor con parametros
            Dim headFunction As String = vbTab & "Sub New("
            Dim footFunction As String = vbTab & "End Sub"

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumnsSinAuditoria.Rows(i)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                headFunction = headFunction & _
                                IIf(i = 0, "ByRef ", "ByVal ") & _
                                drColumns.Item("name").ToString & _
                                " As " & _
                                GetTranslateType(drType.Item("name")) & _
                                IIf(i = dtColumnsSinAuditoria.Rows.Count - 1, ")", ", ")
            Next

            objReader.WriteLine(headFunction)

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumnsSinAuditoria.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)

                objReader.WriteLine(vbTab & vbTab & "v" & nameColumn & " = " & nameColumn)
            Next

            objReader.WriteLine(footFunction)

            objReader.WriteLine("")
            objReader.WriteLine("End Class")
            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("")
        


            objReader.WriteLine(" Public Class " + strtabla + "Col   ")
            objReader.WriteLine(" Inherits System.Collections.CollectionBase")
            objReader.WriteLine("Private _enmSortDirection As SortDirection")
            objReader.WriteLine("Public Sub New()")
            objReader.WriteLine("_enmSortDirection = SortDirection.Asc")
            objReader.WriteLine("End Sub")
            objReader.WriteLine("Public Sub Sort(ByVal sortExpression As String, ByVal sortDirection As SortDirection)")
            objReader.WriteLine("InnerList.Sort(New Comparer(sortExpression, SortDirection))")
            objReader.WriteLine("End Sub")
            objReader.WriteLine("Public Sub Sort(ByVal sortExpression As String)")
            objReader.WriteLine("InnerList.Sort(New Comparer(sortExpression, _enmSortDirection))")
            objReader.WriteLine("If _enmSortDirection = SortDirection.Asc Then")
            objReader.WriteLine("_enmSortDirection = SortDirection.Desc")
            objReader.WriteLine("Else")
            objReader.WriteLine("_enmSortDirection = SortDirection.Asc")
            objReader.WriteLine("End If")
            objReader.WriteLine("End Sub")
            objReader.WriteLine("Public Function Add(ByVal obj As " + strtabla + ") As Integer")
            objReader.WriteLine("Return MyBase.List.Add(obj)")
            objReader.WriteLine("End Function")
            objReader.WriteLine("Public Sub AddPrimero(ByVal obj As " + strtabla + ")")
            objReader.WriteLine("MyBase.List.Insert(0, obj)")
            objReader.WriteLine("End Sub")
            objReader.WriteLine("Public ReadOnly Property " + strtabla + "(ByVal Index As Integer) As " + strtabla)
            objReader.WriteLine("Get")
            objReader.WriteLine("Return CType(MyBase.List(Index), " + strtabla + ")")
            objReader.WriteLine("End Get")
            objReader.WriteLine("End Property")
            objReader.WriteLine("End Class")
            objReader.WriteLine("end namespace")






            objReader.WriteLine("")


            objReader.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub GenerateClassDE_VBNET(ByRef drTable As DataRow)
        Try


            Dim nameTable As String = drTable.Item("name").ToString
            Dim strNamespace As String = ""
            Dim strtabla As String = ""
            Try
                strNamespace = NamespaceTraduccion(nameTable.Split("_")(0))
                strtabla = nameTable.Split("_")(1)
            Catch ex As Exception
                strNamespace = "general"
                strtabla = nameTable
            End Try


            Dim strUbicacion As String = Me.txtCarpetaDestino.Text & "\DE"

            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If

            strUbicacion = strUbicacion & "\" & strNamespace
            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If



            Dim objReader As New StreamWriter(strUbicacion & "\" & strtabla & ".vb")


            objReader.WriteLine("")

            'Obtenemos las columnas de la tabla
            Dim dtColumns As DataTable = GetColumns(CInt(drTable.Item("object_id")))
            Dim dtColumnsSinAuditoria As DataTable = GetColumnsSinAuditoria(CInt(drTable.Item("object_id")))

            'Obtiene el PK de la tabla, en caso de no tener PK se obtiene la primera columna
            Dim drPrimaryKey As DataRow = GetPrimaryKey(CInt(drTable.Item("object_id")))
            Dim nameColumnPK As String = ""
            If drPrimaryKey Is Nothing Then
                drPrimaryKey = dtColumns.Rows(0)
                nameColumnPK = dtColumns.Rows(0).Item("name").ToString
            Else
                nameColumnPK = drPrimaryKey.Item("name").ToString
            End If
            nameColumnPK = nameColumnPK.Substring(0, 1).ToUpper & nameColumnPK.Substring(1)

            nameTable = nameTable.Substring(0, 1).ToUpper & nameTable.Substring(1)


            objReader.WriteLine("Imports System.Data")
            objReader.WriteLine("Imports System.Data.SqlClient")
            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("Namespace " + strNamespace)
            objReader.WriteLine("Public Class " & strtabla)


            objReader.WriteLine("")

            objReader.WriteLine("public Function Listar(" + nameColumnPK + " as integer) as Entity." & strNamespace & "." & strtabla)
            objReader.WriteLine("")
            objReader.WriteLine("Dim strNameProcedure as string = ""up_" + nameTable + "_obtener""")
            objReader.WriteLine("Dim sqlReader As SqlDataReader = Nothing")
            objReader.WriteLine("Dim sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)")
            objReader.WriteLine("dim objEnt as   entity." + strNamespace + "." + strtabla)
            objReader.WriteLine("Try")
            objReader.WriteLine("            sqlCon.Open()")
            objReader.WriteLine("Dim sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)")
            objReader.WriteLine("With sqlCmd")
            objReader.WriteLine(".CommandType = CommandType.StoredProcedure")
            objReader.WriteLine("                .Parameters.Add(New SqlParameter(""@" + nameColumnPK + """, SqlDbType.Int)).Value =" + nameColumnPK)
            objReader.WriteLine("            End With")
            objReader.WriteLine("            sqlReader = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)")

            objReader.WriteLine("If sqlReader.HasRows Then")
            objReader.WriteLine("While sqlReader.Read")

            objReader.WriteLine(" objEnt= new    entity." + strNamespace + "." + strtabla)

            objReader.WriteLine("With objEnt")
            '.Codigo = Integer.Parse(sqlDr("usu_id").ToString)

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)

                objReader.WriteLine(vbTab & vbTab & "." & nameColumn & " = sqlReader(""" & nameColumn & """).ToString")


            Next



            objReader.WriteLine("End With")
            objReader.WriteLine("  End While")
            objReader.WriteLine("  End IF")
            objReader.WriteLine(" Catch ex As Exception")
            objReader.WriteLine("Throw New Exception(ex.ToString)")
            objReader.WriteLine("Finally")
            objReader.WriteLine(" sqlReader.Dispose()")
            objReader.WriteLine("  sqlReader.Close()")
            objReader.WriteLine("  sqlReader = Nothing")
            objReader.WriteLine("  sqlCon.Dispose()")
            objReader.WriteLine(" sqlCon.Close()")
            objReader.WriteLine(" sqlCon = Nothing")
            objReader.WriteLine(" End Try")

            objReader.WriteLine("return objEnt")
            objReader.WriteLine("")
            objReader.WriteLine("End Function")


            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("")

            objReader.WriteLine("public Function Listar(Opcion as string , _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name"), "VBNET", True) & IIf(i < dtColumnsSinAuditoria.Rows.Count - 1, ", _", ") _"))


            Next




            objReader.WriteLine("as  Entity." & strNamespace & "." & strtabla & "Col")
            objReader.WriteLine("")

            objReader.WriteLine("Dim strNameProcedure as string = ""up_" + nameTable + "_listar""")
            objReader.WriteLine("dim objCol as   entity." + strNamespace + "." + strtabla + "Col")

            objReader.WriteLine("Try")
            objReader.WriteLine("Using conn As New SqlConnection(Configuracion.Connexion.Cnn)")
            objReader.WriteLine("Dim cmd As New SqlCommand(strNameProcedure, conn)")
            objReader.WriteLine("cmd.CommandType = CommandType.StoredProcedure")


            objReader.WriteLine("  cmd.Parameters.Add(New SqlParameter(""@opcion"", SqlDbType.varchar,50)).Value =Opcion")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(" cmd.Parameters.Add(New SqlParameter(""@" + nameColumn.ToString + """,SqlDbType." + GetFormatColumnData(drColumns, True).ToUpper + ")).Value =" + nameColumn.ToString)

            Next
            objReader.WriteLine("conn.Open()")
            objReader.WriteLine("Using dr As SqlDataReader = cmd.ExecuteReader()")

            For i As Integer = 0 To dtColumns.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & "dim i" & nameColumn & " as integer = dr.GetOrdinal(""" & nameColumn & """)")


            Next
            objReader.WriteLine("")
            objReader.WriteLine(" Dim colCount As Integer = dr.FieldCount")
            objReader.WriteLine(" Dim values As Object() = New Object(colCount - 1) {}")
            objReader.WriteLine("")
            objReader.WriteLine(" objCol =  New entity." + strNamespace + "." + strtabla + "Col")

            objReader.WriteLine("  If dr.HasRows Then")
            objReader.WriteLine("While dr.Read()")
            objReader.WriteLine("dim objEnt =  new entity." + strNamespace + "." + strtabla)
            objReader.WriteLine("dr.GetValues(values)")

            objReader.WriteLine("")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)

                objReader.WriteLine(vbTab & vbTab & "objEnt." & nameColumn & " = values(i" & nameColumn & ")")


            Next


            objReader.WriteLine(" objCol.Add(objEnt)")
            objReader.WriteLine(" End While")
            objReader.WriteLine(" End if")

            objReader.WriteLine(" End Using")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" Catch ex As Exception")
            objReader.WriteLine("Throw New Exception(ex.ToString)")
            objReader.WriteLine("Finally")
            objReader.WriteLine(" End Try")
            objReader.WriteLine("return objCol")
            objReader.WriteLine("")
            objReader.WriteLine("End Function")



            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")


            objReader.WriteLine("Public  Function Insert(ByVal opcion As String, _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1

                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name")) & ", _")
            Next
            objReader.WriteLine("vUsuario as integer ) As Integer")
            objReader.WriteLine("")
            objReader.WriteLine(" dim strNameProcedure as string =""Up_" + nameTable + "_Insertar""")

            objReader.WriteLine(" Try")
            objReader.WriteLine("Dim intCodigo As Integer = -1")
            objReader.WriteLine(" Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)")
            objReader.WriteLine("Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)")
            objReader.WriteLine("With sqlCmd")
            objReader.WriteLine(" .CommandType = CommandType.StoredProcedure")
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@opcion"", SqlDbType.VarChar, 120)).Value = opcion")


            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@" + nameColumn.ToString + """,SqlDbType." + GetFormatColumnData(drColumns).ToUpper + ")).Value =" + nameColumn.ToString)

            Next

            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@_usuario"", SqlDbType.Int)).Value = vUsuario")
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@_fecha"", SqlDbType.BigInt)).Value = now.tofiletime()")

            objReader.WriteLine(".Parameters.Add(New SqlParameter(""@variable_out"", SqlDbType.Int))")
            objReader.WriteLine(" .Parameters(""@variable_out"").Direction = ParameterDirection.Output")
            objReader.WriteLine(" sqlCon.Open()")
            objReader.WriteLine(" .ExecuteNonQuery()")
            objReader.WriteLine(" intCodigo = .Parameters(""@variable_out"").Value")
            objReader.WriteLine("  Return intCodigo")
            objReader.WriteLine("  End With")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" Catch ex As Exception")
            objReader.WriteLine("  Throw New Exception(ex.Message)")
            objReader.WriteLine(" Return -1")
            objReader.WriteLine(" End Try")
            objReader.WriteLine("")
            objReader.WriteLine("End Function")

            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("Public  Function Update(ByVal opcion As String, _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1

                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name")) & ", _")
            Next
            objReader.WriteLine("vUsuario as integer ) As Integer")
            objReader.WriteLine("")
            objReader.WriteLine(" dim strNameProcedure as string =""Up_" + nameTable + "_Actualizar""")

            objReader.WriteLine(" Try")
            objReader.WriteLine("Dim intCodigo As Integer = -1")
            objReader.WriteLine(" Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)")
            objReader.WriteLine("Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)")
            objReader.WriteLine("With sqlCmd")
            objReader.WriteLine(" .CommandType = CommandType.StoredProcedure")
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@opcion"", SqlDbType.VarChar, 120)).Value = opcion")


            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@" + nameColumn.ToString + """,SqlDbType." + GetFormatColumnData(drColumns).ToUpper + ")).Value =" + nameColumn.ToString)

            Next
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@_usuario"", SqlDbType.Int)).Value = vUsuario")
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@_fecha"", SqlDbType.BigInt)).Value = now.tofiletime()")

            objReader.WriteLine(".Parameters.Add(New SqlParameter(""@variable_out"", SqlDbType.Int))")
            objReader.WriteLine(" .Parameters(""@variable_out"").Direction = ParameterDirection.Output")
            objReader.WriteLine(" sqlCon.Open()")
            objReader.WriteLine(" .ExecuteNonQuery()")
            objReader.WriteLine(" intCodigo = .Parameters(""@variable_out"").Value")
            objReader.WriteLine("  Return intCodigo")
            objReader.WriteLine("  End With")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" Catch ex As Exception")
            objReader.WriteLine("  Throw New Exception(ex.Message)")
            objReader.WriteLine(" Return -1")
            objReader.WriteLine(" End Try")
            objReader.WriteLine("")
            objReader.WriteLine("End Function")

            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")



            objReader.WriteLine("Public Shared Function Eliminar(ByVal opcion As String, " & nameColumnPK & " as integer, vusuario as integer) as integer")


            objReader.WriteLine("")
            objReader.WriteLine(" dim strNameProcedure as string =""Up_" + nameTable + "_Eliminar""")

            objReader.WriteLine(" Try")
            objReader.WriteLine("Dim intCodigo As Integer = -1")
            objReader.WriteLine(" Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)")
            objReader.WriteLine("Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)")
            objReader.WriteLine("With sqlCmd")
            objReader.WriteLine(" .CommandType = CommandType.StoredProcedure")
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@opcion"", SqlDbType.VarChar, 120)).Value = opcion")
            objReader.WriteLine("                .Parameters.Add(New SqlParameter(""@" + nameColumnPK + """, SqlDbType.Int)).Value =" + nameColumnPK)

            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@_usuario"", SqlDbType.Int)).Value = vUsuario")
            objReader.WriteLine(" .Parameters.Add(New SqlParameter(""@_fecha"", SqlDbType.BigInt)).Value = now.tofiletime()")

            objReader.WriteLine(".Parameters.Add(New SqlParameter(""@variable_out"", SqlDbType.Int))")
            objReader.WriteLine(" .Parameters(""@variable_out"").Direction = ParameterDirection.Output")
            objReader.WriteLine(" sqlCon.Open()")
            objReader.WriteLine(" .ExecuteNonQuery()")
            objReader.WriteLine(" intCodigo = .Parameters(""@variable_out"").Value")
            objReader.WriteLine("  Return intCodigo")
            objReader.WriteLine("  End With")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" End Using")
            objReader.WriteLine(" Catch ex As Exception")
            objReader.WriteLine("  Throw New Exception(ex.Message)")
            objReader.WriteLine(" Return -1")
            objReader.WriteLine(" End Try")
            objReader.WriteLine("")
            objReader.WriteLine("End Function")

            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")


            objReader.WriteLine("")
            objReader.WriteLine("End Class")
            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("")



            objReader.WriteLine("end namespace")






            objReader.WriteLine("")


            objReader.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub GenerateClassLE_VBNET(ByRef drTable As DataRow)
        Try

            Dim nameTable As String = drTable.Item("name").ToString
            Dim strNamespace As String = ""
            Dim strtabla As String = ""
            Try
                strNamespace = NamespaceTraduccion(nameTable.Split("_")(0))
                strtabla = nameTable.Split("_")(1)
            Catch ex As Exception
                strNamespace = "general"
                strtabla = nameTable
            End Try


            Dim strUbicacion As String = Me.txtCarpetaDestino.Text & "\LE"

            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If

            strUbicacion = strUbicacion & "\" & strNamespace
            If Not Directory.Exists(strUbicacion) Then
                Directory.CreateDirectory(strUbicacion)
            End If



            Dim objReader As New StreamWriter(strUbicacion & "\" & strtabla & ".vb")



            objReader.WriteLine("")

            'Obtenemos las columnas de la tabla
            Dim dtColumns As DataTable = GetColumns(CInt(drTable.Item("object_id")))
            Dim dtColumnsSinAuditoria As DataTable = GetColumnsSinAuditoria(CInt(drTable.Item("object_id")))

            'Obtiene el PK de la tabla, en caso de no tener PK se obtiene la primera columna
            Dim drPrimaryKey As DataRow = GetPrimaryKey(CInt(drTable.Item("object_id")))
            Dim nameColumnPK As String = ""
            If drPrimaryKey Is Nothing Then
                drPrimaryKey = dtColumns.Rows(0)
                nameColumnPK = dtColumns.Rows(0).Item("name").ToString
            Else
                nameColumnPK = drPrimaryKey.Item("name").ToString
            End If
            nameColumnPK = nameColumnPK.Substring(0, 1).ToUpper & nameColumnPK.Substring(1)

            nameTable = nameTable.Substring(0, 1).ToUpper & nameTable.Substring(1)


            ' objReader.WriteLine("Imports System.Data")
            'objReader.WriteLine("Imports System.Data.SqlClient")
            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("Namespace " + strNamespace)
            objReader.WriteLine("Public Class " & strtabla)

            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine(" Public Function Combo(ByVal vTodos As Boolean, Optional ByVal vmensaje As String = ""Todos"") As Entity." & strNamespace & "." & strtabla & "Col")

            objReader.WriteLine("dim objCol as  New entity." + strNamespace + "." + strtabla + "Col")
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)
            objReader.WriteLine("objCol = obj.Listar("""", _")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                Dim valueColumn As String = GetDefaultValue(drType.Item("name"))
                objReader.WriteLine(vbTab & vbTab & valueColumn & IIf(i < dtColumnsSinAuditoria.Rows.Count - 1, ", _", ")"))

            Next

            objReader.WriteLine(" If vTodos Then")
            objReader.WriteLine("Dim ent As New Entity." & strNamespace & "." & strtabla & "( _ ")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))
                Dim valueColumn As String = GetDefaultValue(drType.Item("name"))

                Select Case i
                    Case 0
                        objReader.WriteLine(vbTab & vbTab & "-1, _")
                    Case 1
                        If dtColumnsSinAuditoria.Rows.Count = 2 Then
                            objReader.WriteLine(vbTab & vbTab & "vmensaje )")
                        Else
                            objReader.WriteLine(vbTab & vbTab & "vmensaje , _")
                        End If

                    Case Else
                        objReader.WriteLine(vbTab & vbTab & valueColumn & IIf(i < dtColumnsSinAuditoria.Rows.Count - 1, ", _", ")"))
                End Select


            Next

            objReader.WriteLine("objCol.AddPrimero(ent)")
            objReader.WriteLine("End If")

            objReader.WriteLine("Return objCol")
            objReader.WriteLine("End Function")


            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("public Function Listar(" + nameColumnPK + " as integer) as Entity." & strNamespace & "." & strtabla)
            objReader.WriteLine("")

            objReader.WriteLine("dim objEnt as  New entity." + strNamespace + "." + strtabla)
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)


            objReader.WriteLine("return obj.Listar(" + nameColumnPK + ")")
            objReader.WriteLine("")
            objReader.WriteLine("End Function")


            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("")


            objReader.WriteLine("public Function Listar( _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name"), "VBNET", True) & IIf(i < dtColumnsSinAuditoria.Rows.Count - 1, ", _", ") _"))


            Next
            objReader.WriteLine("as  Entity." & strNamespace & "." & strtabla & "Col")
            objReader.WriteLine("")
            objReader.WriteLine("dim objCol as  New entity." + strNamespace + "." + strtabla + "Col")
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)


            objReader.WriteLine("return obj.listar("""", _")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & IIf(i < dtColumnsSinAuditoria.Rows.Count - 1, ", _", ")"))

            Next



            objReader.WriteLine("")
            objReader.WriteLine("End Function")


            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("public Function Grabar( _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name"), "VBNET", True) & ", _")


            Next

            objReader.WriteLine("vUSuario as integer)as  integer")
            objReader.WriteLine("")
            objReader.WriteLine("dim _codigo as integer =-1")
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)

            objReader.WriteLine(" if " & nameColumnPK & "=-1 then ")
            'insert
            objReader.WriteLine("_codigo= obj.insert( """", _")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " , _")
            Next
            objReader.WriteLine(" vUSuario )")

            'usuario

            'update
            objReader.WriteLine("else")
            objReader.WriteLine("_codigo= obj.update( """", _")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " , _")
            Next
            objReader.WriteLine(" vUSuario )")

            objReader.WriteLine("end if ")





            objReader.WriteLine("return _codigo")


            objReader.WriteLine("")
            objReader.WriteLine("End Function")


            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("public Function Insertar( _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name"), "VBNET", True) & ", _")


            Next

            objReader.WriteLine("vUSuario as integer)as  integer")
            objReader.WriteLine("")
            objReader.WriteLine("dim _codigo as integer =-1")
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)


            'insert
            objReader.WriteLine("_codigo= obj.insert( """", _")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & ", _")
            Next
            objReader.WriteLine(" vUSuario )")


            objReader.WriteLine("return _codigo")


            objReader.WriteLine("")
            objReader.WriteLine("End Function")


            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("public Function Updatear( _")

            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))


                objReader.WriteLine(vbTab & vbTab & nameColumn & " as " & GetTranslateType(drType.Item("name"), "VBNET", True) & ", _")

            Next

            objReader.WriteLine("vUSuario as integer)as  integer")
            objReader.WriteLine("")
            objReader.WriteLine("dim _codigo as integer =-1")
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)


            'insert
            objReader.WriteLine("_codigo= obj.Update( """", _")
            For i As Integer = 0 To dtColumnsSinAuditoria.Rows.Count - 1
                Dim drColumns As DataRow = dtColumns.Rows(i)

                Dim nameColumn As String = drColumns.Item("name").ToString
                nameColumn = nameColumn.Substring(0, 1).ToUpper & nameColumn.Substring(1)
                Dim drType As DataRow = GetTypeColumn(CInt(drColumns.Item("system_type_id")))

                objReader.WriteLine(vbTab & vbTab & nameColumn & " , _")
            Next
            objReader.WriteLine(" vUSuario )")


            objReader.WriteLine("return _codigo")


            objReader.WriteLine("")
            objReader.WriteLine("End Function")




            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("'***************************")
            objReader.WriteLine("")
            objReader.WriteLine("")

            objReader.WriteLine("public Function ELiminar(  " + nameColumnPK + " as integer,vusuario as integer ,optional vopcion as string=""Delete"") as integer")

      
            objReader.WriteLine("dim _codigo as integer =-1")
            objReader.WriteLine("dim obj as  New Data." + strNamespace + "." + strtabla)


            'insert
            objReader.WriteLine("_codigo= obj.Eliminar( ""vopcion"", " + nameColumnPK + " ,vusuario)")
            

            objReader.WriteLine("return _codigo")


            objReader.WriteLine("")
            objReader.WriteLine("End Function")




            objReader.WriteLine("")
            objReader.WriteLine("End Class")
            objReader.WriteLine("")
            objReader.WriteLine("")
            objReader.WriteLine("")



            objReader.WriteLine("end namespace")






            objReader.WriteLine("")


            objReader.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    Private Sub btnChktodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChktodos.Click
        For Each dr As DataGridViewRow In Me.dgvTablas.Rows
            dr.Cells(0).Value = 1
        Next
    End Sub

    Public Function NamespaceTraduccion(ByVal vstring As String)
        Dim _Str As String = ""

        Select Case vstring.ToUpper
            Case "CRM"
                _Str = "Cliente"
            Case "SEC"
                _Str = "Seguridad"
            Case "KAR"
                _Str = "Kardex"
            Case "PRO"
                _Str = "Producto"
            Case "VEN"
                _Str = "Ventas"
            Case "PER"
                _Str = "Personal"
            Case Else
                _Str = vstring

        End Select





        Return _Str

    End Function
End Class